package com.beta.cms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.beta.cms.common.until.DataModel;
import com.beta.cms.common.until.ResultMapUtils;
import com.beta.cms.common.until.excel.ExcelXlsxUtils;
import com.beta.cms.controller.base.BaseController;
import com.beta.cms.model.entity.Product;
import com.beta.cms.model.entity.User;
import com.beta.cms.service.UserService;

@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/list")
	public ModelAndView findList() {
		ModelAndView mav = new ModelAndView("/user");
		User user = new User();
		user.setId(1l);
		user.setName("张三");
		int result = userService.save(user);
		mav.addObject("user", user);
		this.logger.info("================" + result + "=====================");
		return mav;
	}

	@RequestMapping(value = "/queryById")
	public DataModel<Object> queryById(Long id) {
		User user = userService.queryById(id);
		return ResultMapUtils.getResultMap(user);
	}

	@RequestMapping(value = "/save")
	public String save() {
		User user = new User();
		user.setName("王宝蛇");
		userService.save(user);
		return "success";
	}

	@RequestMapping(value = "/query")
	public String query() {
		userService.queryAnnotation();
		return "success";
	}

	@RequestMapping(value = "/queryE")
	public String queryE(String name) {
		User user = userService.queryEhcache(name);
		this.logger.info("=================" + user.getName() + "=====================");
		return "success";
	}

	@RequestMapping(value = "/exportExcel")
	public String exportExcel(HttpServletRequest request, HttpServletResponse response) {
		String str = (String) request.getParameter("text");
		this.logger.info("=================" + str + "=====================");
		List<Product> list = new ArrayList<Product>();
		for (int i = 0; i < 10; i++) {
			Product p1 = new Product();
			p1.setId((long) i);
			p1.setName("张三" + i);
			p1.setMoney((double) 12 + i);
			list.add(p1);
		}
		try {
			ExcelXlsxUtils.getWorkBook(list, response, "lossFollowUpPatient测试.xlsx");
			// ExcelXlsUtils.getWorkBook(list, response,
			// "lossFollowUpPatient测试.xls");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";

	}
}
